//
//  UserAccount.swift
//  WeiBoText
//
//  Created by 刘世瑛 on 16/1/16.
//  Copyright © 2016年 刘世瑛. All rights reserved.
//

import UIKit

class UserAccount: NSObject,NSCoding {
    //用于调用access_token,接口获取授权后的access_token
    var access_token:String?
    //access_token的生命周期，时间是s
    var expires_in:NSTimeInterval? = 0
    
    var uid:String?
    var name : String?
    var avatar_large:String?
    init(dict:[String:AnyObject])
    {
        super.init()
        
        setValuesForKeysWithDictionary(dict)
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
    }
    
    
     func saveInfor()
    {
        let path = (NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("account.plist")
        NSKeyedArchiver.archiveRootObject(self, toFile: path)
        print(path)
    }
    class func loadUserInfor() ->UserAccount?
    {
        
        let  path = (NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("account.plist")
        if let account = NSKeyedUnarchiver.unarchiveObjectWithFile(path) as? UserAccount{
            return account
        }
        
        return nil
       
    }
    required init?(coder aDecoder: NSCoder) {
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
         expires_in = aDecoder.decodeDoubleForKey("expires_in")
         uid = aDecoder.decodeObjectForKey("uid") as? String
        name = aDecoder.decodeObjectForKey("name") as? String

        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String

        
    }
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(access_token, forKey: "access_token")
          aCoder.encodeObject(expires_in, forKey: "expires_in")
          aCoder.encodeObject(uid, forKey: "uid")
          aCoder.encodeObject(name, forKey: "name")
        aCoder.encodeObject(avatar_large, forKey: "avatar_large")
    }
}
